package kr.hkit.powerjava.chapter12;

import java.util.Arrays;
import java.util.Comparator;
class Student{
	
	private String stdName;
	private double gpa;
	public Student(String stdName, double gpa) {
		this.stdName = stdName;
		this.gpa = gpa;
	}
	
	public String getStdName() {
		return stdName;
	}

	public void setStdName(String stdName) {
		this.stdName = stdName;
	}

	public double getGpa() {
		return gpa;
	}

	public void setGpa(double gpa) {
		this.gpa = gpa;
	}

	@Override
	public String toString() {
		return String.format("[%s, %s]",stdName,gpa);
	}

/*	@Override
	public int compareTo(Student o) {
		
		return (int)(this.gpa-o.gpa);
	}
	*/
}

public class SortTest {
	public static void main(String[] args) {
		int[] arr={5,6,7,8,1,2,3,4};
		
		prnArr(arr);
		Arrays.sort(arr);
		Student[] arrStd =new Student[4];
		arrStd[0] = new Student("홍길동",3.19);
		arrStd[1] = new Student("임꺽정",4.19);
		arrStd[2] = new Student("황진이",2.19);
		arrStd[3] = new Student("김태희",1.19);
		
//		prnArr(arrStd);
		//이름순정렬
//		Arrays.sort(arrStd);
		Comparator<Student> compName = new Comparator<Student>() {
			public int compare(Student o1,Student o2){
				return o1.getStdName().hashCode()-o2.getStdName().hashCode();
			}
		};
		//평점순정렬
		Comparator<Student> compGpa = new Comparator<Student>(){

			@Override
			public int compare(Student o1, Student o2) {
				// TODO Auto-generated method stub
				return (int)(o1.getGpa()-o2.getGpa());
			}
			
		};
		Arrays.sort(arrStd, compName);
		prnArr(arrStd);
		Arrays.sort(arrStd, compGpa);
		prnArr(arrStd);
	}
	

	private static void prnArr(Student[] arrStd) {
		for(Student s: arrStd){
			System.out.print(s+"\t");
		}
		System.out.println();
		// TODO Auto-generated method stub
		
	}

	private static void prnArr(int[] arr) {
		for(int e:arr){
			System.out.printf("%2d",e);
		}
		System.out.println();
		
	}

}
